﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using DataRecv.tools;
using DataRecv.db;
using System.Data;

namespace DataRecv.models.road
{
    /// <summary>
    /// 路口参数
    /// </summary>
    public class RoadParam
    {
        /// <summary>
        /// 路口编号
        /// </summary>
        public string roadid;
        /// <summary>
        /// 地点代码
        /// </summary>
        public string gbdm;
        /// <summary>
        /// 路口名称
        /// </summary>
        public string roadname;
        /// <summary>
        /// 卡口图片保存路径
        /// </summary>
        public string kkpath;
        /// <summary>
        /// 电警图片保存路径
        /// </summary>
        public string djpath;
        /// <summary>
        /// 卡口网络虚拟路径
        /// </summary>
        public string kkvpath;
        /// <summary>
        /// 电警网络虚拟路径
        /// </summary>
        public string djvpath;
        /// <summary>
        /// 部门编号
        /// </summary>
        public string bmbh;
        /// <summary>
        /// 行政区划
        /// </summary>
        public string xzqh;
        /// <summary>
        /// 备注
        /// </summary>
        public string bz;
        /// <summary>
        /// 表名称
        /// </summary>
        private const string c_TableName = "RoadParam_t";
        /// <summary>
        /// 字段集合
        /// </summary>
        private const string c_Columns = "roadid,gbdm,roadname,kkpath,djpath,kkvpath,djvpath,bmbh,xzqh,bz";
        /// <summary>
        /// 字段数量
        /// </summary>
        private const int c_ColumnsCount = 10;

        

        /// <summary>
        /// 空构造函数
        /// </summary>
        public RoadParam()
        {
            roadid = "";
            gbdm = "";
            roadname = "";
            kkpath = "";
            djpath = "";
            kkvpath = "";
            djvpath = "";
            bmbh = "";
            xzqh = "";
            bz = "";
            
        }

        /// <summary>
        /// 查询所有路口
        /// </summary>
        /// <returns></returns>
        public static List<RoadParam> GetModels()
        {
            List<RoadParam> ans = null;
            DataTable dt = null;
            try
            {
                dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "1=1 order by RoadParam_t.roadid");
                if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count > 0))
                {
                    ans = new List<RoadParam>();
                    foreach (DataRow row in dt.Rows)
                    {
                        RoadParam road = new RoadParam();
                        road.roadid = row["roadid"].ToString();
                        road.gbdm = row["gbdm"].ToString();
                        road.roadname = row["roadname"].ToString();
                        road.kkpath = row["kkpath"].ToString();
                        road.djpath = row["djpath"].ToString();
                        road.kkvpath = row["kkvpath"].ToString();
                        road.djvpath = row["djvpath"].ToString();
                        road.bmbh = row["bmbh"].ToString();
                        road.xzqh = row["xzqh"].ToString();
                        road.bz = row["bz"].ToString();
                        ans.Add(road);
                    }
                }
            }
            catch (Exception er)
            {
                ans = null;
                LogTool.log.Info("RoadParam.GetModels()" + er.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
            return ans;
        }

        /// <summary>
        /// 添加路口
        /// </summary>
        /// <param name="road">路口</param>
        /// <returns>结果标识</returns>
        public static bool AddModel(RoadParam road)
        {
            bool ans = false;
            try
            {
                string[] values = new string[c_ColumnsCount];
                values[0] = road.roadid;
                values[1] = road.gbdm;
                values[2] = road.roadname;
                values[3] = road.kkpath;
                values[4] = road.djpath;
                values[5] = road.kkvpath;
                values[6] = road.djvpath;
                values[7] = road.bmbh;
                values[8] = road.xzqh;
                values[9] = road.bz;
                ans = DBHelper.GetInstance().Add(c_TableName, c_Columns, values);
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("RoadParam.AddModel()" + er.Message);
            }
            return ans;
        }

        /// <summary>
        /// 查询路口
        /// </summary>
        /// <param name="roadid">路口编号</param>
        /// <returns>路口模型</returns>
        public static RoadParam GetModel(string roadid)
        {
            RoadParam ans = null;
            DataTable dt = null;
            try
            {
                if ((roadid != null) && (roadid != ""))
                {
                    dt = DBHelper.GetInstance().Select(c_TableName, c_Columns, "roadid='" + roadid + "'");
                    if ((dt != null) && (dt.Rows != null) && (dt.Rows.Count == 1))
                    {
                        ans = new RoadParam();
                        ans.roadid = dt.Rows[0]["roadid"].ToString();
                        ans.gbdm = dt.Rows[0]["gbdm"].ToString();
                        ans.roadname = dt.Rows[0]["roadname"].ToString();
                        ans.kkpath = dt.Rows[0]["kkpath"].ToString();
                        ans.djpath = dt.Rows[0]["djpath"].ToString();
                        ans.kkvpath = dt.Rows[0]["kkvpath"].ToString();
                        ans.djvpath = dt.Rows[0]["djvpath"].ToString();
                        ans.bmbh = dt.Rows[0]["bmbh"].ToString();
                        ans.xzqh = dt.Rows[0]["xzqh"].ToString();
                        ans.bz = dt.Rows[0]["bz"].ToString();
                    }
                }
            }
            catch (Exception er)
            {
                ans = null;
                LogTool.log.Info("RoadParam.GetModel()" + er.Message);
            }
            finally
            {
                if (dt != null) dt.Dispose();
            }
            return ans;
        }

        /// <summary>
        /// 修改路口
        /// </summary>
        /// <param name="roadid">路口模型</param>
        /// <returns>结果标志</returns>
        public static bool EditModel(RoadParam road)
        {
            bool ans = false;
            try
            {
                string[] values = new string[c_ColumnsCount];
                values[0] = road.roadid;
                values[1] = road.gbdm;
                values[2] = road.roadname;
                values[3] = road.kkpath;
                values[4] = road.djpath;
                values[5] = road.kkvpath;
                values[6] = road.djvpath;
                values[7] = road.bmbh;
                values[8] = road.xzqh;
                values[9] = road.bz;
                ans = DBHelper.GetInstance().Edit(c_TableName, c_Columns, values, "roadid='" + road.roadid + "'");
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("RoadParam.EditModel()" + er.Message);
            }
            return ans;
        }

        /// <summary>
        /// 删除路口
        /// </summary>
        /// <param name="roadid">路口状态</param>
        /// <returns>结果标志</returns>
        public static bool DeleteModel(string roadid)
        {
            bool ans = false;
            try
            {
                if ((roadid != null) && (roadid != ""))
                {
                    ans = DBHelper.GetInstance().Delete(c_TableName, "roadid='" + roadid + "'");
                }
            }
            catch (Exception er)
            {
                ans = false;
                LogTool.log.Info("RoadParam.DeleteModel()" + er.Message);
            }
            return ans;
        }
    }
}
